/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.app.client.widgets;

import org.nuxeo.gwt.habyt.app.client.Application;
import org.nuxeo.gwt.habyt.app.client.View;
import org.nuxeo.gwt.habyt.invocation.client.DataModel;
import org.nuxeo.gwt.habyt.invocation.client.Referenceable;
import org.nuxeo.gwt.habyt.templates.client.TemplatePanel;
import org.nuxeo.gwt.habyt.templates.client.bindings.Helper;

import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A container of views
 * 
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class ViewWidget extends View {

    protected Referenceable input;

    public ViewWidget() {
        initDefaults();
    }

    public SimplePanel getPanel() {
        return (SimplePanel) getWidget();
    }

    @Override
    protected Widget createWidget() {
        return new SimplePanel();
    }

    protected void initDefaults() {
        setSize("100%", "100%");
    }

    @Override
    public String getId() {
        return Helper.getIdAttribute(getElement());
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        if (getId() != null) {
            Application.get().addView(this);
        }
    }

    @Override
    protected void onUnload() {
        super.onUnload();
        if (getId() != null) {
            Application.get().removeView(this);
        }
    }

    protected Widget createContent(Referenceable input) {
        TemplatePanel panel = new TemplatePanel();
        panel.load(input.getRef());
        return panel;
    }

    public void doOpen(Referenceable ref) {
        if (ref == null) {
            getPanel().setWidget(null);
            input = null;
            return;
        }
        input = ref;
        getPanel().setWidget(createContent(input));
    }

    public Referenceable getActiveInput() {
        return input;
    }

    public void close() {
        getPanel().setWidget(null);
        input = null;
    }

    public boolean hasInput() {
        return input != null;
    }

    public void closeAll() {
        close();
    }

    public void reloadActiveView() {
        if (input != null) {
            getPanel().setWidget(createContent(input));
        }
    }

    public String getInputTitle(Referenceable input) {
        if (input instanceof DataModel) {
            return ((DataModel) input).getTitle();
        }
        return input.getRef().getId();
    }

}
